package org.xukai.common.log;


import org.springframework.util.ResourceUtils;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.web.util.WebUtils;
import org.xukai.common.util.PlaceHolderUtils;
import org.xukai.common.util.SystemPropertiesUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author xukai
 * @desc
 * @date 2016-09-11-下午 1:28
 */
public class Log4jConfigListener extends org.springframework.web.util.Log4jConfigListener{

    private static final String LOG4j_PROPERTIES_PARAM = "log4jProperties";
    private static final String DEFAULT_LOG4j_PROPERTIES = "log.properties";

    public Log4jConfigListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        Properties properties = this.getProperties(servletContext);
        Map map = SystemPropertiesUtil.setSystemProperties(properties);
//        Iterator var6 = map.entrySet().iterator();
//
//        while(var6.hasNext()) {
//            Map.Entry entry = (Map.Entry)var6.next();
////            servletContext.log("Set property: \'" + (String)entry.getKey() + "\' = [" + (String)entry.getValue() + "]");
//        }

        super.contextInitialized(event);
    }

    private Properties getProperties(ServletContext servletContext) {
        String param = servletContext.getInitParameter("log4jProperties");
        String location = param != null?param:"log.properties";
        Properties properties = new Properties();
        FileInputStream inputStream = null;

        try {
            if(!ResourceUtils.isUrl(location)) {
                location = SystemPropertyUtils.resolvePlaceholders(location);
                location = WebUtils.getRealPath(servletContext, location);
            }

            File ex = ResourceUtils.getFile(location);
            if(!ex.exists()) {
                if(param != null) {
                    throw new FileNotFoundException("Log properties file [" + location + "] not found");
                }
            } else {
                inputStream = new FileInputStream(ex);
                properties.load(inputStream);
                PlaceHolderUtils.convertProperties(properties);
            }
        } catch (FileNotFoundException var15) {
            throw new IllegalArgumentException("Invalid \'log4jProperties\' parameter:" + var15.getMessage());
        } catch (IOException var16) {
            throw new IllegalArgumentException("Invalid \'log4jProperties\' parameter:" + var16.getMessage());
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException var14) {
                    ;
                }
            }

        }

        return properties;
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);
        Properties properties = this.getProperties(event.getServletContext());
        SystemPropertiesUtil.removeSystemProperties(properties);
    }
}
